package com.unSafe;

import java.util.Set;
import java.util.UUID;
import java.util.concurrent.CopyOnWriteArraySet;

// ConcurrentModificationException: 并发修改异常
public class SetTest {
    public static void main(String[] args) {
//        Set<String> set = new HashSet<>();  // 底层为：HashMap
        // public HashSet() {
        //        map = new HashMap<>();
        //    }
        // 解决方案一：利用集合工具类  转换为synchronized
//        Set<String> set = Collections.synchronizedSet(new HashSet<>());
        // 解决方案二：CopyOnWriteArraySet
        Set<String> set = new CopyOnWriteArraySet<>();

        for (int i = 1; i <= 10; i++) {
            new Thread(() -> {
                set.add(UUID.randomUUID().toString().substring(0, 5));
                System.out.println(set);
            }, String.valueOf(i)).start();
        }
    }
}
